﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using XmlServer.ServiceContracts;
using XmlServer.ServiceImplementation;


namespace DelayOperator
{
    public class Operator : IOperator
    {

        public const string SERVER_NAME = "Delay Operator";
        public const string SERVER_VERSION = "1.0";

        public string Name
        {
            get { return SERVER_NAME; }
        }

        public string Version
        {
            get { return SERVER_VERSION; }
        }

        public string FullName
        {
            get { return String.Format("{0}_{1}", SERVER_NAME, Version); }
        }

        public string Description
        {
            get { return "This operator will sleep the operator thread for the specified amount of time.  Useful for testing and load/stress testing the Xml Server."; }
        }

        public List<IFact> Facts
        {
            get 
            {
                return new List<IFact>
                {
                    new Fact { Name = "Delay", IsRequired = true, Description = "The amount of time to delay the operator." },
                    new Fact { Name = "DelayType", IsRequired = true, Description = "The type of time value for the Delay fact.  Valid values are ms, sec, mins" }
                };
            }
        }


        public IResponse ProcessRequest(IRequest request)
        {
            Response response = new Response();
            response.Request = request;     // return the original request object
            response.Success = true;        // assume a success

            try
            {
                // get the "Facts" we need from the request
                var delay = XmlServer.Helper.Utilities.GetFact<int>(request.Facts, "Delay");
                var delayType = XmlServer.Helper.Utilities.GetFact(request.Facts, "DelayType");

                if (delayType.Equals("ms", StringComparison.InvariantCultureIgnoreCase))
                {
                    Thread.Sleep(delay);
                }
                else if (delayType.Equals("secs", StringComparison.InvariantCultureIgnoreCase))
                {
                    Thread.Sleep(delay * 1000);
                }
                else if (delayType.Equals("mins", StringComparison.InvariantCultureIgnoreCase))
                {
                    Thread.Sleep(delay * 1000 * 60);
                }
                else
                {
                    response.Facts = XmlServer.Helper.Utilities.CreateException("Invalid Request: Fact 'DelayType' must be ms, sec, mins");
                    response.Success = false;
                }                

            }
            catch (Exception ex)
            {
                response.Facts = XmlServer.Helper.Utilities.CreateException(ex);
                response.Success = false;
            }

            response.StopProcessing = System.Environment.TickCount;
            return response;
        }

    }
}
